# -*- coding: utf-8 -*-
# Copyright © 2015 Beijing HengXingChangDa Technology Co., Ltd. All rights reserved.


from mjs.settings import DEBUG
from mjs.config import *
import datetime
import sys


def KEIS_ASSERT(condition):
    if DEBUG:
        assert condition

def kerror(*args, **kwargs):
    try:
        print(*args, file=sys.stderr, **kwargs)
    except:
        for log in args:
            print(str(log).encode('utf-8'), file=sys.stderr, **kwargs)

def kinfo(*args, **kwargs):
    try:
        print(*args, **kwargs)
    except:
        for log in args:
            print(str(log).encode('utf-8'), **kwargs)

def kdebug3(*args, **kwargs):
    if not DEBUG or mjs_debug_level < 3:
        return
    kinfo(*args, **kwargs)

def kdebug2(*args, **kwargs):
    if not DEBUG or mjs_debug_level < 2:
        return
    kinfo(*args, **kwargs)

def kdebug(*args, **kwargs):
    if not DEBUG:
        return
    kinfo(*args, **kwargs)


def make_enums(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)


def make_date(t=None):
    if t is None:
        d = datetime.datetime.now()
    else:
        if isinstance(t, float) or isinstance(t, int):
            d = datetime.datetime.fromtimestamp((int(t)-25569)*86400)
        else:
            d = datetime.datetime.strptime(t, KEIS_DATE_FORMAT)
    return d.date()


def make_batch(b):
    if isinstance(b, float):
        b = str(int(b))
    else:
        if isinstance(b, int):
            b = str(b)
    return b.strip()
